<template>
  <page-header-wrapper>
    <a-card class="search-card  m-b-20" :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="14" :lg="12" :xl="8" :sm="24">
              <a-form-item label="角色名称">
                <a-input v-model="queryParam.RoleName" placeholder="请输入角色名称" @pressEnter="$refs.table.refresh(true)" />
              </a-form-item>
            </a-col>
            <a-col :md="10" :lg="8" :xl="8" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="handleReset">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        :rowKey="(record) => record.ID"
        :columns="columns"
        :data="loadData"
        :scroll="{x: true}"
        :pagination="{ showTotal: total => `共 ${total} 条` }"
      >
        <!-- 表格操作栏 -->
        <template slot="tabAction">
          <a-button type="primary" icon="plus" @click="handleAdd">新增角色</a-button>
        </template>
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <span slot="remark" slot-scope="text">
          <ellipsis :length="30" tooltip>{{ text }}</ellipsis>
        </span>
        <span slot="date" slot-scope="text">{{ text | moment }}</span>

        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="handleEdit(record)">修改</a>
            <a-divider type="vertical" />
            <a-popconfirm
              title="您确定是否删除当前角色?"
              ok-text="是"
              cancel-text="否"
              placement="bottomLeft"
              @confirm="handleRemove(record.ID)"
            >
              <a-icon slot="icon" type="question-circle-o" />
              <a href="#">删除</a>
            </a-popconfirm>
          </template>
        </span>
      </s-table>
    </a-card>
    <edit-role :visible.sync="visibles" :propsData="propsData"></edit-role>
  </page-header-wrapper>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import { getRoleList, delRoles } from '@/api/system'
import EditRole from './edit'
const columns = [
  {
    title: '序号',
    align: 'center',
    width: 60,
    scopedSlots: { customRender: 'serial' }
  },
  {
    title: '角色名称',
    align: 'left',
    dataIndex: 'RoleName',
    width: 160
  },
  {
    title: '备注',
    align: 'left',
    dataIndex: 'Remark',
    width: 160,
    scopedSlots: { customRender: 'remark' }
  },
  {
    title: '创建时间',
    align: 'left',
    dataIndex: 'CreateTime',
    width: 160,
    scopedSlots: { customRender: 'date' }
  },
  {
    title: '更新时间',
    align: 'left',
    dataIndex: 'UpdateTime',
    width: 160,
    scopedSlots: { customRender: 'date' }
  },
  {
    title: '操作',
    align: 'left',
    dataIndex: 'action',
    width: 150,
    fixed: 'right',
    scopedSlots: { customRender: 'action' }
  }
]

export default {
  name: 'RoleList',
  components: {
    STable,
    Ellipsis,
    EditRole
  },
  data () {
    this.columns = columns
    return {
      visibles: false,
      // 查询参数
      queryParam: {
        UserID: this.$store.getters.userid,
        RoleName: ''
      },
      propsData: {
        type: 'add',
        id: ''
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return getRoleList(requestParameters).then(res => {
          return res
        })
      }
    }
  },
  methods: {
    handleAdd () {
      this.visibles = true
      this.propsData = { type: 'add' }
    },
    handleEdit (row) {
      this.visibles = true
      this.propsData = { row, type: 'update' }
    },
    handleRemove (ID) {
      delRoles({
        roleid: ID,
        rz: {
          ActionNo: '2',
          Actuation: '删除角色',
          NewValue: '',
          OldValue: '',
          People: this.$store.getters.nickname
        }
      }).then(() => {
        this.$message.success('角色删除成功', 2.5)
      }).catch(() => {
        this.$message.error('角色删除失败, 请稍后重试', 2.5)
      }).finally(() => {
        this.$refs.table.refresh()
      })
    },
    handleReset () {
      this.queryParam = {
        UserID: this.$store.getters.userid,
        RoleName: ''
      }
      this.$refs.table.refresh()
    }
  }
}
</script>
